package com.googlecode.pacswitch.conditions;

import java.nio.channels.AsynchronousSocketChannel;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlType;

import com.googlecode.pacswitch.Condition;
import com.googlecode.pacswitch.ObjectFactory;
import com.googlecode.pacswitch.PacSwitch;

/**
 * @author Andrey Pavlenko
 */
@XmlType(name = "And", namespace = ObjectFactory.CNS)
public class And implements Condition {
    private final List<JAXBElement<Condition>> _conditions = new ArrayList<>();

    @XmlAnyElement(lax = true)
    public List<JAXBElement<Condition>> getConditions() {
        return _conditions;
    }

    @Override
    public boolean apply(final PacSwitch ps,
            final AsynchronousSocketChannel clientSocket) {
        for (final JAXBElement<Condition> c : getConditions()) {
            if (!c.getValue().apply(ps, clientSocket)) {
                return false;
            }
        }
        return true;
    }
}
